<template>
  <div>
    <el-dialog :title="detailData.id?'编辑权限':'添加权限'" :visible="isShowDialog" @close="btnCancel">
      <el-form ref="form" :model="formData" label-width="80px" :rules="rules">
        <el-form-item label="权限名称" prop="title">
          <el-input v-model="formData.title" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
      </el-form>
      <el-row slot="footer">
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { addPerm, editPerm } from '@/api/permissions'
export default {
  props: {
    isShowDialog: {
      type: Boolean,
      default: false
    },
    permConfig: {
      type: Object,
      required: true
    },
    detailData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      formData: {
        title: '',
        code: '',
        description: ''
      },
      rules: {
        title: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 50, message: '最多不能超过50个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 50, message: '最多不能超过50个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 50, message: '最多不能超过50个字符', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    detailData() {
      this.formData = this.detailData
    }
  },
  methods: {
    async  btnOK() {
    //   console.log(this.formData)
      console.log({ ...this.formData, ...this.permConfig })
      if (this.detailData.id) {
        //  编辑
        await editPerm({ ...this.formData, id: this.detailData.id })
        this.$message.success('修改成功')
        this.detailData.id = ''
      } else {
        //  新增
        await addPerm({ ...this.formData, ...this.permConfig })
        this.$message.success('添加成功')
      }
      this.$emit('update:isShowDialog', false)
      this.formData = {}
      this.$emit('reloadPage')
    },
    btnCancel() {
      this.detailData.id = ''
      this.$emit('update:isShowDialog', false)
      this.formData = {}
    }
  }

}
</script>

<style>

</style>
